package com.tucc.hello.leetcode.no1_400.no1_200.no121;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.maxProfit(new int[]{2, 9, 4, 1, 7}));
    }

    public int maxProfit(int[] prices) {
        int min = prices[0];
        int max = prices[0];
        int mmax = 0;
        for (int i = 1; i < prices.length; i++) {
            int price = prices[i];
            if (price < min) {
                mmax = Math.max(max - min, mmax);
                max = price;
                min = price;
            } else {
                max = Math.max(max, price);
            }
        }
        return Math.max(max - min, mmax);
    }


}
